﻿/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Data;
using Votations.NSurvey.BusinessRules;
using Votations.NSurvey.WebAdmin.UserControls;


namespace Votations.NSurvey.WebAdmin
{
    /// <summary>
    /// Filter edition page
    /// </summary>
    public partial class FilterEditor : PageBase
    {
 

        protected void Page_Load(object sender, System.EventArgs e)
        {
            SetupSecurity();
            LocalizePage();

            if (!Page.IsPostBack)
            {
                SurveyList.SurveyId = SurveyId;
                SurveyList.BindDropDown();
              
                // Init the user control with the current survey id
                FilterOption.SurveyId = SurveyId;
                BindFields();
            }

            if (FilterDropDownList.SelectedValue != "0")
            {
                FilterOption.FilterId = int.Parse(FilterDropDownList.SelectedValue);
                FilterOption.Visible = true;
            }
            else
            {
                FilterOption.Visible = false;
            }

            FilterOption.OptionChanged += new EventHandler(FilterOption_OptionChanged);
        }

        private void SetupSecurity()
        {
            CheckRight(NSurveyRights.CreateResultsFilter, true);
        }

        private void LocalizePage()
        {
            FilterBuilderTitle.Text = GetPageResource("FilterBuilderTitle");
            SelectFilteraLabel.Text = GetPageResource("SelectFilteraLabel");
            CreateFilterHyperLink.Text = GetPageResource("CreateFilterHyperLink");
        }

        /// <summary>
        ///	Triggered by the filter option user control
        ///	in case anything was changed we will need to
        ///	rebind to display the updates to the user 
        /// </summary>
        void FilterOption_OptionChanged(object sender, EventArgs e)
        {
            BindFields();
        }


        private void BindFields()
        {
            CreateFilterHyperLink.NavigateUrl = UINavigator.FilterCreator + "?surveyid=" + SurveyId + "&menuindex=" + MenuIndex;
            FilterDropDownList.DataSource = new Filters().GetFilters(SurveyId);
            FilterDropDownList.DataMember = "Filters";
            FilterDropDownList.DataTextField = "Description";
            FilterDropDownList.DataValueField = "FilterID";
            FilterDropDownList.DataBind();
            FilterDropDownList.Items.Insert(0,
                new ListItem(GetPageResource("SelectFilterMessage"), "0"));
            if (FilterDropDownList.Items.Count < 2)
            {
                FilterDropDownList.Visible = false;
            }

        }


 

        protected void FilterDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            FilterOption.BindFields();
        }

    }

}
